// @flow

import invariant from "invariant";
import type { CryptoCurrency } from "../types";
import { getEnv } from "../env";
import { explorerConfig } from "./explorerConfig";

type LedgerExplorer = {
  version: string,
  id: string,
  endpoint: string,
};

export const findCurrencyExplorer = (
  currency: CryptoCurrency
): ?LedgerExplorer => {
  const config = explorerConfig[currency.id];
  if (!config) return;
  const { id } = config;
  if (getEnv("SATSTACK") && currency.id === "bitcoin") {
    return { endpoint: getEnv("EXPLORER_SATSTACK"), id: "btc", version: "v3" };
  }
  if (config.experimental && getEnv("EXPERIMENTAL_EXPLORERS")) {
    const { base, version } = config.experimental;
    return { endpoint: getEnv(base), id, version };
  }
  const { base, version } = config.stable;
  return { endpoint: getEnv(base), id, version };
};

export const hasCurrencyExplorer = (currency: CryptoCurrency): boolean =>
  !!findCurrencyExplorer(currency);

export const getCurrencyExplorer = (
  currency: CryptoCurrency
): LedgerExplorer => {
  const res = findCurrencyExplorer(currency);
  invariant(res, `no Ledger explorer for ${currency.id}`);
  return res;
};

export const blockchainBaseURL = (currency: CryptoCurrency): string => {
  const { id, version, endpoint } = getCurrencyExplorer(currency);
  return `${endpoint}/blockchain/${version}/${id}`;
};
